/**
  EG4Sim project, Anyang Normal University && IMP-CAS
  \class EG4DC
  \brief the model of the DC downstream of the dipole magnet
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2023-11-08
  \date 2023-11-08 last modified
  \copyright Copyright (c) 2023-2024 IMP-CAS with LGPLv3 LICENSE
*/

#include "EG4DC.h"
#include "G4Box.hh"
#include "G4Material.hh"
#include "G4LogicalVolume.hh"
#include "G4VisAttributes.hh"
#include "G4PVPlacement.hh"
#include "G4Colour.hh"
#include "G4SystemOfUnits.hh"

EG4DC::EG4DC() : EG4Det(){
  G4Material *dcgas = G4Material::GetMaterial("c20Lanzhou", true);
  G4Material *Al5052 = G4Material::GetMaterial("Al5052", true);
  G4Material *FR4 = G4Material::GetMaterial("FR4", true);

  fLV = new G4LogicalVolume(new G4Box("DC", 490.*mm, 430.*mm, 65.*mm), dcgas, "DC");
  SetColor(0., 1., 0.); // green
  // add the insensitive supporting frame wrapping the sense wires //
  auto frmX_Al = new G4LogicalVolume(new G4Box("frm_DC_Al_X", 40.*mm, 430.*mm, 5.*mm), Al5052, "frm_DC_Al_X");
  auto frmY_Al = new G4LogicalVolume(new G4Box("frm_DC_Al_Y", 410.*mm, 65.*mm, 5.*mm), Al5052, "frm_DC_Al_Y");
  auto frmX_fr4 = new G4LogicalVolume(new G4Box("frm_DC_fr4_X", 40.*mm, 430.*mm, 15.*mm), FR4, "frm_DC_fr4_X");
  auto frmY_fr4 = new G4LogicalVolume(new G4Box("frm_DC_fr4_Y", 410.*mm, 65.*mm, 15.*mm), FR4, "frm_DC_fr4_Y");
  frmX_fr4->SetVisAttributes(new G4VisAttributes(G4Colour(.8, .8, 0.))); // yellow
  frmY_fr4->SetVisAttributes(new G4VisAttributes(G4Colour(.8, .8, 0.))); // yellow
  for(int i = 0; i < 2; i++) for(int j = 0; j < 4; j++){
    const int id = i*4 + j;
    new G4PVPlacement(nullptr, G4ThreeVector((1.-2.*i)*450.*mm, 0., (60.-j*40.)*mm),
      frmX_Al, "frm_DC_Al_X", fLV, false, id, true);
    new G4PVPlacement(nullptr, G4ThreeVector(0., (1.-2.*i)*365.*mm, (60.-j*40.)*mm),
      frmY_Al, "frm_DC_Al_Y", fLV, false, id, true);
    if(3 == j) continue;
    new G4PVPlacement(nullptr, G4ThreeVector((1.-2.*i)*450.*mm, 0., (40.-j*40.)*mm),
      frmX_fr4, "frm_DC_fr4_X", fLV, false, id, true);
    new G4PVPlacement(nullptr, G4ThreeVector(0., (1.-2.*i)*365.*mm, (40.-j*40.)*mm),
      frmY_fr4, "frm_DC_fr4_Y", fLV, false, id, true);
  } // end for over i and j

  // wire planes for DCs
  fWLV = new G4LogicalVolume(new G4Box("WireDC", 410.*mm, 300.*mm, 5.*mm),
    dcgas, "WireDC");
  fWLV->SetVisAttributes(new G4VisAttributes(G4Colour(1., 1., 0.))); // yellow

  // due to historical reasons
  // x0 plane's z is the DC's center from laser survey
  const double z0 = 0.; // 5.*mm;
  fWX[0] = new G4PVPlacement(nullptr, G4ThreeVector(0., 0., -5.*mm + z0),
    fWLV, "WireDCX0", fLV, false, 0, true); // X0
  fWX[1] = new G4PVPlacement(nullptr, G4ThreeVector(0., 0., 5.*mm + z0),
    fWLV, "WireDCX1", fLV, false, 1, true); // X1
  fWU[0] = new G4PVPlacement(nullptr, G4ThreeVector(0., 0., -45.*mm + z0),
    fWLV, "WireDCU0", fLV, false, 2, true); // U0
  fWU[1] = new G4PVPlacement(nullptr, G4ThreeVector(0., 0., -35.*mm + z0),
    fWLV, "WireDCU1", fLV, false, 3, true); // U1
  fWV[0] = new G4PVPlacement(nullptr, G4ThreeVector(0., 0., 35.*mm + z0),
    fWLV, "WireDCV0", fLV, false, 4, true); // V0
  fWV[1] = new G4PVPlacement(nullptr, G4ThreeVector(0., 0., 45.*mm + z0),
    fWLV, "WireDCV1", fLV, false, 5, true); // V1
} // end ctor

EG4DC::~EG4DC(){}
